#ifndef JMUTEX_H
#define JMUTEX_H

#include "jthreadpool_global.h"

// - class JMutex -

class JMutexPrivate;
class QMutex;

/**
 * @brief The JMutex class
 */
class JTHREADPOOL_EXPORT JMutex
{
public:
    /**
     * @brief The RecursionMode enum
     */
    enum RecursionMode { NonRecursive, Recursive };

    /**
     * @brief JMutex
     * @param mode
     */
    explicit JMutex(RecursionMode mode = NonRecursive);
    ~JMutex();

    /**
     * @brief lock
     */
    void lock();

    /**
     * @brief tryLock
     * @param timeout
     * @return
     */
    bool tryLock(int timeout = 0);

    /**
     * @brief unlock
     */
    void unlock();

    /**
     * @brief JMutex
     * @param other
     */
    JMutex(const JMutex &other);

    /**
     * @brief operator =
     * @param other
     * @return
     */
    JMutex &operator=(const JMutex &other);

    /**
     * @brief JMutex
     * @param other
     */
    JMutex(QMutex *other);

    /**
     * @brief mutex
     * @return
     */
    QMutex *mutex() const;

private:
    JMutexPrivate *d;
};

// - class JMutexLocker -

class JMutexLockerPrivate;

/**
 * @brief The JMutexLocker class
 */
class JTHREADPOOL_EXPORT JMutexLocker
{
public:

    /**
     * @brief JMutexLocker
     * @param mutex
     */
    explicit JMutexLocker(JMutex *mutex);
    ~JMutexLocker();

    /**
     * @brief unlock
     */
    void unlock();

    /**
     * @brief relock
     */
    void relock();

    /**
     * @brief mutex
     * @return
     */
    JMutex mutex() const;

private:
    J_DISABLE_COPY(JMutexLocker)
    JMutexLockerPrivate *d;
};

#endif // JMUTEX_H
